import {Buffer} from "buffer";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";


export class COMDCB implements AxdrType{

    dataCode : Buffer | null = null;
    baut : AxdrEnum | null = null;
    parity : AxdrEnum | null = null;
    datab : AxdrEnum | null = null;
    stopb : AxdrEnum | null = null;
    flow : AxdrEnum | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(baut : AxdrEnum, parity : AxdrEnum, datab : AxdrEnum, stopb : AxdrEnum, flow : AxdrEnum){
        this.baut = baut;
        this.parity = parity;
        this.datab = datab;
        this.stopb = stopb;
        this.flow = flow;
    }

    setBaut(baut : AxdrEnum){
        this.baut = baut;
    }

    setParity(parity : AxdrEnum){
        this.parity = parity;
    }

    setDatab(datab : AxdrEnum){
        this.datab = datab;
    }

    setStopb(stopb : AxdrEnum){
        this.stopb = stopb;
    }

    setFlow(flow : AxdrEnum){
        this.flow = flow;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.baut = new AxdrEnum();
        this.baut.set_const();
        codeLength += this.baut.decode(input);

        this.parity = new AxdrEnum();
        this.parity.set_const();
        codeLength += this.parity.decode(input);

        this.datab = new AxdrEnum();
        this.datab.set_const();
        codeLength += this.datab.decode(input);

        this.stopb = new AxdrEnum();
        this.stopb.set_const();
        codeLength += this.stopb.decode(input);

        this.flow = new AxdrEnum();
        this.flow.set_const();
        codeLength += this.flow.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.baut != null && this.parity != null && this.datab != null && this.stopb != null && this.flow != null){
                codeLength = 0;
                codeLength += this.flow.encode(output);

                codeLength += this.stopb.encode(output);

                codeLength += this.datab.encode(output);

                codeLength += this.parity.encode(output);

                codeLength += this.baut.encode(output);
            }else{
                throw new Error("flow || stopb || datab || parity || baut is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.baut != null && this.parity != null && this.datab != null && this.stopb != null && this.flow != null) {
            return "sequence: {"+ "baut: " + this.baut + ", parity: " + this.parity + ", datab: " + this.datab + ", stopb: " + this.stopb + ", flow: " + this.flow  + "}";
        } else {
            return "flow or stopb or datab or parity or baut is null";
        }
    }

}
